package com.inmarry.util;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;




public class SendEmail  {


    public void sendRegisterEmail(String activeCode) throws Exception {
        Session session = getMailSession();

        MimeMessage message = new MimeMessage(session);

        //设置from地址
        message.setFrom(new InternetAddress(from));
        //设置to地址
        message.addRecipient(
                Message.RecipientType.TO, 
                new InternetAddress(to));

        //设置邮件主题
        message.setSubject("in-marry.com欢迎您，请激活您的帐户", "UTF-8");

        MimeMultipart   mp   =   new   MimeMultipart();  
        mp.setSubType("related");  
                         
        MimeBodyPart   mbp1=   new   MimeBodyPart();  
        String   html   =    
                 "<html>"+  
                 "<head><title>请激活您的帐户</title></head>"+  
                 "<body>"+  
                 "<h1>欢迎您注册www.in-marry.com" +
                 "请点击<a href=\"http://www.in-marry.com/active.do?code=" + 
                 activeCode +
                 "\">这里</a>激活您的帐户</h1><br>" +
                 "</body>"+  
                 "</html>";  
                  
        mbp1.setContent(html,"text/html;charset=utf-8");  
                                                                                                                                                               
        mp.addBodyPart(mbp1);  
        message.setContent(mp);   
        //设置邮件内容
        message.saveChanges();
        
        Transport transport = session.getTransport("smtp");

        transport.send(message);

    }


    

    public Session getMailSession() {
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.auth", smtpNeedAuth);
        MyAuth myauth = new MyAuth(smtpUser, smtpPassword);

        return Session.getInstance(props, myauth);
    }

    public void setSmtpHost(String smtpHost) {
        this.smtpHost = smtpHost;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public void isSmtpNeedAuth(boolean isNeed) {
        this.smtpNeedAuth = isNeed;
    }

    public void setSmtpUser(String smtpUser) {
        this.smtpUser = smtpUser;
    }

    public void setSmtpPassword(String smtpPassword) {
        this.smtpPassword = smtpPassword;
    }



    //=================================
    //私有字段
    //==================================

    private String smtpHost = "smtp.163.com";
    private String from = "hongweig@163.com";
    private boolean smtpNeedAuth = true;
    private String smtpUser = "hongweig@163.com";
    private String smtpPassword = "79328";

    private String to;






}


class MyAuth extends Authenticator {
    public MyAuth(String user, String password) {
        this.user = user;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }

    private String user;
    private String password;

}

